#!/usr/bin/env python
# encoding: utf-8
#
# PyGadu - Pythonic Gadu-Gadu implementation
# Copyright (c) 2005,2006 Zygmunt Krynicki
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

def ggStringToUnicode(gg_string):
    end = gg_string.find('\0')
    if end == -1:
        return gg_string.decode("cp1250", "replace")
    else:
        crap = gg_string[end+1:]
        if len(crap) > 0:
            print u"[LEFTOVER CRAP]%s[/LEFTOVER CRAP] (%d bytes)" % (crap, len(crap))
        return gg_string[:end].decode("cp1250", "replace")

def unicodeToGgString(string):
    return string.encode("cp1250", "replace")

def passwordHash(password, seed):
    x, y, z = 0L, 0L, 0L
    y = long(seed)
    for c in password.encode("cp1250", "replace"):
        x = (x & 0xFFFFFF00L) | ord(c)
        y ^= x
        y &= 0xFFFFFFFFL
        y += x
        y &= 0xFFFFFFFFL
        x <<= 8;
        x &= 0xFFFFFFFFL
        y ^= x
        y &= 0xFFFFFFFFL
        x <<= 8;
        x &= 0xFFFFFFFFL
        y -= x
        y &= 0xFFFFFFFFL
        x <<= 8;
        x &= 0xFFFFFFFFL
        y ^= x
        y &= 0xFFFFFFFFL
        z = y & 0x1f;
        z &= 0xFFFFFFFFL
        y = (y << z) | (y >> (32 -z))
        y &= 0xFFFFFFFFL
    return y 


